
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

#ifndef IPRIMITIVE_H
#define IPRIMITIVE_H

#include "CRay.h"
#include "CMatrix.h"

class CMaterial;


class IPrimitive
{
protected:
	CMaterial* m_material;

	//world matrix (position of the primitive in world coordinate)
	CMatrix m_worldMatrix;

	//inverse world matrix
	CMatrix m_inverseWorldMatrix;

	//world view (position of the primitive in camera coordinate
	CMatrix m_worldViewMatrix;

	//inverse world view matrix
	CMatrix m_inverseWorldViewMatrix;
public:
	//constructor
	IPrimitive()
	{
		m_worldViewMatrix.identity(); 
		m_inverseWorldViewMatrix.identity();
		m_worldMatrix.identity(); 
		m_inverseWorldMatrix.identity();
		m_worldViewMatrix.identity(); 
		m_inverseWorldViewMatrix.identity();
	}

	//destructor
	virtual ~IPrimitive(){}

	//check intersection between a ray and the primitive.
	//If globalCoordinates is true, the ray is in global coordinates. If false, it is in view coordinates.
	virtual bool intersect(const CRay& ray, float& dist, bool useGlobalCoordinates) = 0;

	//check if a point is inside the primitive
	virtual bool isInside(const CVector3f&)const{return false;};

	//gives the normal in a point
	virtual void computeNormal(const CRay& ray, float dist, CVector3f& normal) const = 0;

	//settors the primitive's material
	void setMaterial(CMaterial* material){m_material = material;}

	//set a translate, rotate and scale matrix to set the world matrix
	void setMatrices(const CMatrix& T, const CMatrix& R, const CMatrix& S);

	//set the primitive's world matrix
	void setWorldMatrix(const CMatrix& world);

	//get a pointer to the primitive's material
	CMaterial* getMaterial()const{return m_material;}

	//get the inverse world matrix
	CMatrix getInverseWorldMatrix()const{ return m_inverseWorldMatrix;}

	//get the inverse world view matrix
	CMatrix getInverseWorldViewMatrix()const { return m_inverseWorldViewMatrix;}

	//apply the view matrix to the model
	virtual void applyViewMatrix(const CMatrix& view);

	//convert a ray from the view coordinates to the local coordinates of the primtive (where the primitive is at (0, 0, 0)
	void convertRayToLocalCoordinates(const CRay& in, CRay& out)const;

	//convert a ray from global coorindates to local coordinates
	void globalToLocal(const CRay& in, CRay& out)const;

	//convert a normal from the local coordinates (where the primitives is at (0, 0, 0)) to the global coordinates
	void convertNormalToGlobalCoordinates(const CVector3f& in, CVector3f& out)const;

	//convert a point in space from global coordinates to the local coordinates of the primitives
	void convertPointToLocalCoordinates(const CVector3f& in, CVector3f& out)const;
};


#endif